﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Xml;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Kiiro.Common.Entities;
using Kiiro.Logging;
using Kiiro.ExceptionManagement;
using Kiiro.Common.Utility;
using Kiiro.DataAccess;
using Kiiro.Model.Utility;

namespace Kiiro.Model.EventHandlers
{
    public class WebEventReceiver : Microsoft.SharePoint.SPWebEventReceiver
    {
        public override void WebDeleting ( SPWebEventProperties properties )
        {
            KiiroDeleting ( properties.SiteId, properties.ServerRelativeUrl );
            base.WebDeleting ( properties );
        }

        public override void WebDeleted ( SPWebEventProperties properties ) { base.WebDeleted ( properties ); }

        public override void WebMoving ( SPWebEventProperties properties ) { base.WebMoving ( properties ); }

        public override void WebMoved ( SPWebEventProperties properties ) { base.WebMoved ( properties ); }

        public override void SiteDeleting ( SPWebEventProperties properties )
        {
            Guid siteCollectionId = properties.SiteId;
            string serverRelativeUrl = properties.ServerRelativeUrl;

            using ( SPSite elevatedSite = SPHelpers.GetSystemSite ( siteCollectionId ) )
            {
                KiiroSolutionControllerHelpers.RemoveTimerJobs ( siteCollectionId, serverRelativeUrl );

                elevatedSite.AllWebs.AsSafeEnumerable ( ).Where ( x => SPHelpers.IsKiiro ( x ) ).ToList ( ).ForEach ( delegate ( SPWeb web )
                {
                    KiiroDeleting ( siteCollectionId, web.ServerRelativeUrl );
                } );
            }

            base.SiteDeleting ( properties );
        }

        public override void SiteDeleted ( SPWebEventProperties properties ) { base.SiteDeleted ( properties ); }

        private void KiiroDeleting ( Guid siteCollectionId, string serverRelativeUrl )
        {
            if ( SPHelpers.IsPortal ( siteCollectionId, serverRelativeUrl ) )
            {
                using ( SPSite elevatedSite = SPHelpers.GetSystemSite ( siteCollectionId ) )
                {
                    using ( SPWeb web = elevatedSite.OpenWeb ( serverRelativeUrl ) )
                    {
                        if ( web.Webs.Count == 0 )
                        {
                            PermissionsModel.RemoveCustomGroupsFromSharePoint ( siteCollectionId, serverRelativeUrl, web.Title );

                            int countOfPortals = elevatedSite.WebApplication.Sites.AsSafeEnumerable().Sum (
                                x => x.AllWebs.AsSafeEnumerable().Where (
                                    y => SPHelpers.IsPortal ( y ) ).Count ( ) );

                            if ( countOfPortals == 1 )
                            {
                                KiiroSolutionControllerHelpers.RemoveTimerJobs ( siteCollectionId, serverRelativeUrl );
                            }
                        }
                    }
                }
            }
            else if ( SPHelpers.IsProject ( siteCollectionId, serverRelativeUrl ) )
            {
                using ( SPWeb web = SPHelpers.GetSystemWeb ( siteCollectionId, serverRelativeUrl ) )
                {
                    PermissionsModel.RemoveCustomGroupsFromSharePoint ( siteCollectionId, serverRelativeUrl, web.Title );
                }

                Project project = ProjectsModel.GetByServerRelativeWebUrl ( siteCollectionId, serverRelativeUrl );
                if ( project != null )
                {
                    project.EditType = EditType.Deleted;

                    DisableEventFiring ( );
                    ProjectsModel.Save ( project, siteCollectionId, serverRelativeUrl );
                    EnableEventFiring ( );
                }
            }
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.Model/EventHandlers/WebEventHandlers/WebEventReceiver.cs $
 * 
 * 16    1/12/10 9:43a Camerons
 * 
 * 15    6/11/09 3:17p Camerons
 * 
 * 14    30/10/09 12:54p Camerons
 * 
 * 13    15/10/09 5:09p Camerons
 * 
 * 12    15/10/09 1:04p Camerons
 * 
 * 11    15/10/09 10:51a Camerons
 * 
 * 10    15/10/09 9:34a Camerons
 * 
 * 9     10/09/09 12:12p Camerons
 * 
 * 8     10/09/09 10:08a Camerons
 * Added missing call to WebDeleting method
 * 
 * 7     8/09/09 10:51a Camerons
 * Moved KiiroSolutionControllerHelpers to the Kiiro.Model.Utility
 * namespace
 * 
 * 6     4/09/09 10:24a Camerons
 * 
 * 5     31/08/09 11:56a Camerons
 * 
 * 4     17/08/09 10:29a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/